﻿using System;
using RAP.Framework.Libary.DomTemplateBuilder.Element.StringActions;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.StringActions
{
	/// <summary>
	/// 
	/// </summary>
	public class StringIsContainsHandler : StringActionConditionalElementHandler
	{
		public new StringIsContains Element
		{
			get
			{
				return base.Element as StringIsContains;
			}
		}

		public override bool IsCondition ( object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			var exp = this.GetPropertyValue ( rootParamObject, parentParamObject, context ) as string;
			if ( string.IsNullOrEmpty ( exp ) )
			{
				return false;
			}
			var find = ExpressionUtils.GetFieldValue
			(
				this.Element.Find
				, rootParamObject
				, parentParamObject
				, context
				, ( ) => { return this.Element.Find; }
			) as string;

			var startIndex = this.Element.StartIndex;
			var count = this.Element.CheckCount;
			var comparisonType = this.Element.ComparisonType;

			var finds = find.Split ( Consts.STRING_SPLIT_CHAR, StringSplitOptions.RemoveEmptyEntries );
			foreach ( var val in finds )
			{
				if ( exp.IndexOf ( val.Trim ( ), startIndex, count, comparisonType ) >= 0 )
				{
					return true;
				}
			}
			return false;
		}


	}
}
